package com.studyweb.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.studyweb.mapper.PaperScoreMapper;
import com.studyweb.pojo.entity.PaperScore;
import com.studyweb.pojo.entity.PaperScoreDetail;
import com.studyweb.service.PaperScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PaperScoreServiceImpl implements PaperScoreService {
    
    @Autowired
    private PaperScoreMapper paperScoreMapper;
    
    @Override
    public PageInfo<PaperScore> getPage(Integer page, Integer size, String examName, String subject) {
        // 设置分页参数
        PageHelper.startPage(page, size);
        
        // 查询数据
        List<PaperScore> paperScores = paperScoreMapper.selectAllWithPagination(examName, subject);
        
        // 为每个试卷分值对象填充详情数据
        for (PaperScore paperScore : paperScores) {
            List<PaperScoreDetail> details = paperScoreMapper.selectDetailsByPaperScoreId(paperScore.getId());
            paperScore.setDetails(details);
        }
        
        // 返回分页信息
        return new PageInfo<>(paperScores);
    }
    
    @Override
    public PaperScore getById(Long id) {
        PaperScore paperScore = paperScoreMapper.selectById(id);
        if (paperScore != null) {
            // 查询并设置详情数据
            List<PaperScoreDetail> details = paperScoreMapper.selectDetailsByPaperScoreId(id);
            paperScore.setDetails(details);
        }
        return paperScore;
    }
    
    @Transactional
    @Override
    public boolean save(PaperScore paperScore) {
        // 设置创建时间和更新时间
        LocalDateTime now = LocalDateTime.now();
        paperScore.setCreateTime(now);
        paperScore.setUpdateTime(now);
        
        // 插入主表
        int result = paperScoreMapper.insert(paperScore);
        if (result <= 0) {
            return false;
        }
        
        // 插入详情表
        if (paperScore.getDetails() != null && !paperScore.getDetails().isEmpty()) {
            for (PaperScoreDetail detail : paperScore.getDetails()) {
                detail.setPaperScoreId(paperScore.getId());
                detail.setCreateTime(now);
                detail.setUpdateTime(now);
                paperScoreMapper.insertDetail(detail);
            }
        }
        
        return true;
    }
    
    @Transactional
    @Override
    public boolean update(PaperScore paperScore) {
        // 设置更新时间
        paperScore.setUpdateTime(LocalDateTime.now());
        
        // 更新主表
        int result = paperScoreMapper.update(paperScore);
        if (result <= 0) {
            return false;
        }
        
        // 如果details为null，说明前端没有传递详情数据，不需要更新详情
        if (paperScore.getDetails() != null) {
            // 删除原有的详情记录
            paperScoreMapper.deleteDetailsByPaperScoreId(paperScore.getId());
            
            // 插入新的详情记录
            if (!paperScore.getDetails().isEmpty()) {
                for (PaperScoreDetail detail : paperScore.getDetails()) {
                    detail.setPaperScoreId(paperScore.getId());
                    detail.setUpdateTime(LocalDateTime.now());
                    // 如果是新增的记录，需要设置创建时间
                    if (detail.getCreateTime() == null) {
                        detail.setCreateTime(LocalDateTime.now());
                    }
                    paperScoreMapper.insertDetail(detail);
                }
            }
        }
        
        return true;
    }
    
    @Transactional
    @Override
    public boolean delete(Long id) {
        return paperScoreMapper.deleteById(id) > 0;
    }
}